package ui.fragment.intersionfragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.IntersionDetailAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.QuestionDetailBean;
import event.IntersionEvent;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;
import view.CircleImageView;
import view.WrapContentLinearLayoutManager;

/**
 * author : ChenHuiMing
 * date : 2018/9/15
 * email : 907399255@qq.com
 * description : 问答详情页
 */
public class IntersionDetailFragment extends BaseNetFragment {

    public static final String GET_QA = "/Qa/getQa";

    private TextView mTvAsked;
    private TextView mTvPose;
    private RecyclerView mRecyclerView;
    private TextView mTvNanme;
    private CircleImageView mCivLogo;
    private TextView mTvSubject;
    private TextView mTvTeacher;
    private TextView mTvQuestion;
    private TextView mTvTime;
    private List<QuestionDetailBean.ListBean> mListBeans = new ArrayList<>();
    private IntersionDetailAdapter adapter;
    private QuestionDetailBean mDetailBean;

    public static IntersionDetailFragment getInstance() {
        return new IntersionDetailFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, GET_QA)) {
            if (baseBean.code == 200) {
                mTvAsked.setVisibility(View.VISIBLE);
                mDetailBean = (QuestionDetailBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), QuestionDetailBean.class);
                mListBeans.clear();
                mListBeans.addAll(mDetailBean.list);
                adapter.setNewData(mListBeans);
                ImageLoaderUtils.loadCircleImage(getActivity(), mDetailBean.head_image, mCivLogo);
                mTvNanme.setText(mDetailBean.student_name);
                mTvSubject.setText("科目：" + mDetailBean.subject_name);
                mTvTeacher.setText("老师：" + mDetailBean.teacher_name);
                mTvTime.setText(mDetailBean.create_time);
                RichText.initCacheDir(getActivity());
                RichText.from(mDetailBean.qa_content).bind(this)
                        .showBorder(false)
                        .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                        .into(mTvQuestion);
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_intersion_detail;
    }

    @Override
    protected void findViews() {
        mTvAsked = getView(R.id.tv_asked);
        mTvPose = getView(R.id.tv_questions);
        mRecyclerView = getView(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.gray)));
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.item_intersion_detail, mRecyclerView, false);
        mTvNanme = getView(headerView, R.id.tv_name);
        mCivLogo = getView(headerView, R.id.civ_logo);
        mTvSubject = getView(headerView, R.id.tv_subject);
        mTvTeacher = getView(headerView, R.id.tv_teacher);
        mTvQuestion = getView(headerView, R.id.tv_question);
        mTvTime = getView(headerView, R.id.tv_time);

        adapter = new IntersionDetailAdapter(R.layout.item_intersion_detail, mListBeans);
        adapter.addHeaderView(headerView);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void formatViews() {
        mTvAsked.setOnClickListener(this);
        mTvPose.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {
        if (mTvPose == view) {
            IntersionEvent pageEvent = new IntersionEvent();
            pageEvent.rightPager = 1;
            pageEvent.isRightPage = true;
            EventBus.getDefault().post(pageEvent);
        } else if (mTvAsked == view) {
            IntersionEvent pageEvent = new IntersionEvent();
            pageEvent.rightPager = 2;
            pageEvent.isRightPage = true;
            pageEvent.qa_id = mDetailBean.qa_id;
            EventBus.getDefault().post(pageEvent);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(IntersionEvent event) {
        if (event.isDetailQa || (event.rightPager == 0 && event.isRightPage && event.isRefresh)) {
            Map<String, Object> map = new HashMap<>();
            map.put("qa_id", event.qa_id);
            post(GET_QA, map, BaseBean.class, false);
        }
    }

}
